﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using LayeredArchitectureSample.DataTransferObjects;
using LayeredArchitectureSample.DomainModel;
using LayeredArchitectureSample.Infrastructure;
using LayeredArchitectureSample.ServiceContracts;

namespace LayeredArchitectureSample.Services
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    [ExceptionMarshallingBehavior]
    public class RemoteLocationRepository : IRemoteLocationRepository
    {
        private readonly ILocationRepository _repository;

        public RemoteLocationRepository(ILocationRepository repository)
        {
            if (repository == null) throw new ArgumentNullException("repository");

            _repository = repository;
        }

        public void RemoveLocation(long id)
        {
            _repository.RemoveLocation(id);
        }

        public LocationDto AddNewLocation(string name)
        {
            var newLocation = _repository.AddNewLocation(name);

            return new LocationDto(newLocation);
        }

        public IEnumerable<LocationDto> GetAll()
        {
            return new List<LocationDto>(_repository.GetAll().Select(x => new LocationDto(x)));
        }
    }
}